package personal.xjl.plusdemo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import personal.xjl.plusdemo.domain.Student;
import personal.xjl.plusdemo.domain.Teacher;
import personal.xjl.plusdemo.service.TeacherService;

import java.sql.Wrapper;
import java.util.List;

@Controller
@RequestMapping("Teacher")
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @RequestMapping("list.do")
    public String list(Model model,@RequestParam(value = "currPage",defaultValue = "1") int currPage,@RequestParam(value = "size",defaultValue = "3")int size) {
       /* List<Teacher> teacherList = teacherService.getAllTeachers();
        model.addAttribute("teachers", teacherList);*/

        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
       // queryWrapper.
        Page<Teacher> page = teacherService.page(new Page<Teacher>(currPage,size));
        model.addAttribute("page",page);
        return "listTeachers";
    }
    @RequestMapping("delete.do")
    //  @ResponseBody
    public String delete(@RequestParam("id") int id, Model model) {
        teacherService.removeById(id);
        return "redirect:/Teacher/list.do";
    }
    @RequestMapping("page.do")
    public String page(@RequestParam("page") String page) {
        return page;
    }
    @RequestMapping("add.do")
    public String add(Teacher teacher) {
        System.out.println(teacher);
        teacherService.save(teacher);
        return "redirect:/Teacher/list.do";
    }
    //准备修改学生信息
    @RequestMapping("toEdit.do")
    public String edit(@RequestParam("id") int id, Model model) {
        Teacher oldStudent=teacherService.getById(id);
        model.addAttribute("teacher",oldStudent);
        return "editTeacher";
    }
    //修改学生信息
    @RequestMapping("edit.do")
    public String edit(Teacher teacher) {
        teacherService.saveOrUpdate(teacher);
        return "redirect:/Teacher/list.do";
    }
        //登录,如果登录成功，跳转到list.do，显示学生数据；如果登录失败，回到login页面
        @RequestMapping("login.do")
        public String login(HttpSession session, @RequestParam("username") String username, @RequestParam("password") String password) {
            if(teacherService.login(username,password)) {
                //发放登录的令牌，username
                session.setAttribute("username",username);
                return "redirect:/Student/list.do";
            }
            else
                return "login";
        }
    @RequestMapping("loginNew.do")
    public String loginNew() {
        return "login";
    }
}

